package vip.indo.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: tianlianyin
 * @Date: 2024/11/27 15:28
 * @Descripition:
 */
@Configuration
public class FastJsonConfig extends WebMvcConfigurationSupport {
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        com.alibaba.fastjson.support.config.FastJsonConfig config = new com.alibaba.fastjson.support.config.FastJsonConfig();
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,//输出空置字段
                SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为""
                SerializerFeature.WriteNullNumberAsZero,//数值字段如果为null,输出为0
                SerializerFeature.WriteNullListAsEmpty,//List字段如果为null,输出为[]
                SerializerFeature.WriteNullBooleanAsFalse,//Boolean字段如果为null,输出为false
                SerializerFeature.DisableCircularReferenceDetect//禁用循环引用
        );
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        List<MediaType> mediaTypeList = new ArrayList<>();//添加支持的MediaType类型
        mediaTypeList.add(MediaType.APPLICATION_JSON);//添加JSON类型
        converter.setSupportedMediaTypes(mediaTypeList);//设置支持的MediaType类型
        converters.add(converter);
    }
}
